package com.crop.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.crop.back.common.enums.Deleted;
import com.crop.back.common.enums.RoleEnum;
import com.crop.back.common.utils.ResultBean;
import com.crop.back.common.utils.UserHolder;
import com.crop.back.dto.UserDTO;
import com.crop.back.entity.Processor;
import com.crop.back.entity.ProductCategory;
import com.crop.back.entity.Supplier;
import com.crop.back.service.ProcessorService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.annotation.Resource;

/**
 * <p>
 * 加工企业表 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/processor")
public class ProcessorController {

    @Resource
    private ProcessorService processorService;

    @GetMapping("/get")
    public ResultBean<Processor> getInfo() {
        UserDTO user = UserHolder.getUser();
        Processor one = processorService.getOne(new LambdaQueryWrapper<Processor>()
                .eq(Processor::getDeleted, Deleted.NOT_DELETED.getCode())
                .eq(Processor::getId, user.getReferenceId()));
        return ResultBean.ok(one);
    }

    @PostMapping("/save")
    public ResultBean<String> update(@RequestBody Processor processor) {
        processorService.updateById(processor);
        return ResultBean.ok("修改成功");
    }

    @GetMapping("/name")
    public ResultBean<List<Processor>> getName() {
        UserDTO user = UserHolder.getUser();
        List<Processor> list = processorService.list(new LambdaQueryWrapper<Processor>()
                .eq(Processor::getDeleted, Deleted.NOT_DELETED.getCode())
                .eq(user.getReferenceId() != RoleEnum.ADMIN.getCode(),Processor::getId, user.getReferenceId()));
        return ResultBean.ok(list);
    }
 
}
